<template>
  <!-- 我是app -->
  <!-- 路由的页面 -->
  <div class="container">
    <div id="box" class="box">
      <div class="img">
        <img src="../assets/vue.svg" alt="" />
      </div>
      <!-- 编写form el-form 表单整体  rules 需要是一个对相关,对象的字段和model的字段要一样 -->
      <el-form label-width="80px" ref="ruleForRef" :model="loginForm" :rules="rules">
        <!-- 子类 -->
        <el-form-item label="用户名" prop="username">
          <!-- prefix-icon 前边添加图标 -->
          <!-- suffix-icon 后边添加图标 -->
          <el-input :prefix-icon="UserFilled" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input :prefix-icon="Lock" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <!-- 提交按钮 -->
        <el-form-item>
          <el-button :plain="true" @click="submitForm">登录</el-button>
          <el-button @click="restForm">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
// 图标
import request from '../util/request'
import { Lock, UserFilled } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import type { FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import type { userp } from '@/api/user/type'
import type { responseUserData } from '@/api/login/type'
import { useUserStore } from '@/store/user'
const store = useUserStore()
const loginForm = reactive<userp>({
  username: 'admin',
  password: '123456',
})

const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入正确的用户名', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入正确的密码', trigger: 'blur' },
    { min: 3, max: 6, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
})

const ruleForRef = ref()
const router = useRouter()
const submitForm = () => {
  ruleForRef.value.validate(async (istrue: any) => {
    if (!istrue) {
      return ElMessage.error('请填写正确的表单')
    }
    const { data, meta }: responseUserData = await request({
      url: '/login',
      method: 'POST',
      data: loginForm,
    })
    if (meta.status !== 200) {
      return ElMessage.error('网络异常,稍后重试')
    }
    // 保存数据到仓库
    store.saveUser(data)
    router.push('/home')
    ElMessage({ message: '登录成功', type: 'success' })
  })
}
// 重置表单
const restForm = () => {
  ruleForRef.value.resetFields()
}
</script>

<style scoped lang="scss">
.container {
  padding-top: 200px;
  height: 100vh;
  background-color: #2B4B6B;

  .box {
    position: relative;
    left: 0;
    top: 0;
    border: 1px solid transparent;
    border-radius: 10px;
    width: 450px;
    height: 310px;
    margin: auto;
    background-color: white;

    .img {
      // x,y 阴影大小,阴影范围,颜色
      box-shadow: 0 0 10px #ddd;
      box-sizing: border-box;
      background-color: white;
      border-radius: 50%;
      padding: 10px;
      // 超出父类,隐藏
      overflow: hidden;
      position: absolute;
      left: 50%;
      // 移动
      transform: translate(-50%, -50%);
      width: 150px;
      height: 150px;

      img {
        width: 100%;
        height: 100%;
        background-color: white;
        border-radius: 50%;
      }
    }

    .el-form {
      margin-top: 100px;
    }

    .el-form-item {
      width: 400px;
    }
  }
}
</style>
